﻿using System.Web.Mvc;

namespace MvcTemplates
{
    public class LabelAttribute : MetadataAttribute
    {
        /// <summary>
        /// this property is here just for convience.  You can still use <see cref="System.ComponentModel.DisplayNameAttribute"/> if you would like.
        /// </summary>
        public string Text { get; set; }
        
        /// <summary>
        /// this property is here in case you want to validate that the field is required (through <see cref="System.ComponentModel.DataAnnotations.RequiredAttribute" />), 
        /// but you do not want to show the html tag (<see ref="MvcTemplates.RequiredTagBuilder"/>) that indicates it is a required field
        /// </summary>
        public bool HideRequiredTag { get; set; }

        /// <summary>
        /// Renders the label after the field.
        /// </summary>
        public bool RenderAfterField { get; set; }

        /// <summary>
        /// Renders the label inside of the field wrapper instead of the normal wrapper
        /// </summary>
        public bool PutInFieldContainer { get; set; }

        /// <summary>
        /// prevents the label and it's wrapper from being rendered
        /// </summary>
        public bool DoNotRender { get; set; }

        public override void Process( ModelMetadata modelMetadata )
        {
            if ( !string.IsNullOrEmpty( Text ) )
            {
                modelMetadata.DisplayName = Text;
            }
            
            if ( DoNotRender )
                modelMetadata.DisplayName = "";
        }
    }
}